package org.jeecg.modules.system.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import kotlin.jvm.internal.Lambda;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.system.entity.District;
import org.jeecg.modules.system.service.IDistrictService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: district
 * @Author: jeecg-boot
 * @Date:   2022-12-23
 * @Version: V1.0
 */
@Api(tags="district")
@RestController
@RequestMapping("/system/district")
@Slf4j
public class DistrictController extends JeecgController<District, IDistrictService> {
     @Autowired
     private IDistrictService districtService;

//	/**
//	 * 分页列表查询
//	 *
//	 * @param district
//	 * @param pageNo
//	 * @param pageSize
//	 * @param req
//	 * @return
//	 */
//	//@AutoLog(value = "district-分页列表查询")
//	@ApiOperation(value="district-分页列表查询", notes="district-分页列表查询")
//	@GetMapping(value = "/list")
//	public Result<IPage<District>> queryPageList(District district,
//								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
//								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
//								   HttpServletRequest req) {
//		QueryWrapper<District> queryWrapper = QueryGenerator.initQueryWrapper(district, req.getParameterMap());
//		Page<District> page = new Page<District>(pageNo, pageSize);
//		IPage<District> pageList = districtService.page(page, queryWrapper);
//		return Result.OK(pageList);
//	}
//
//	/**
//	 *   添加
//	 *
//	 * @param district
//	 * @return
//	 */
//	@AutoLog(value = "district-添加")
//	@ApiOperation(value="district-添加", notes="district-添加")
//	//@RequiresPermissions("org.jeecg.modules:district:add")
//	@PostMapping(value = "/add")
//	public Result<String> add(@RequestBody District district) {
//		districtService.save(district);
//		return Result.OK("添加成功！");
//	}
//
//	/**
//	 *  编辑
//	 *
//	 * @param district
//	 * @return
//	 */
//	@AutoLog(value = "district-编辑")
//	@ApiOperation(value="district-编辑", notes="district-编辑")
//	//@RequiresPermissions("org.jeecg.modules:district:edit")
//	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
//	public Result<String> edit(@RequestBody District district) {
//		districtService.updateById(district);
//		return Result.OK("编辑成功!");
//	}
//
//	/**
//	 *   通过id删除
//	 *
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "district-通过id删除")
//	@ApiOperation(value="district-通过id删除", notes="district-通过id删除")
//	//@RequiresPermissions("org.jeecg.modules:district:delete")
//	@DeleteMapping(value = "/delete")
//	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
//		districtService.removeById(id);
//		return Result.OK("删除成功!");
//	}
//
//	/**
//	 *  批量删除
//	 *
//	 * @param ids
//	 * @return
//	 */
//	@AutoLog(value = "district-批量删除")
//	@ApiOperation(value="district-批量删除", notes="district-批量删除")
//	//@RequiresPermissions("org.jeecg.modules:district:deleteBatch")
//	@DeleteMapping(value = "/deleteBatch")
//	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
//		this.districtService.removeByIds(Arrays.asList(ids.split(",")));
//		return Result.OK("批量删除成功!");
//	}
//
//	/**
//	 * 通过id查询
//	 *
//	 * @param id
//	 * @return
//	 */
//	//@AutoLog(value = "district-通过id查询")
//	@ApiOperation(value="district-通过id查询", notes="district-通过id查询")
//	@GetMapping(value = "/queryById")
//	public Result<District> queryById(@RequestParam(name="id",required=true) String id) {
//		District district = districtService.getById(id);
//		if(district==null) {
//			return Result.error("未找到对应数据");
//		}
//		return Result.OK(district);
//	}
//
//    /**
//    * 导出excel
//    *
//    * @param request
//    * @param district
//    */
//    //@RequiresPermissions("org.jeecg.modules:district:exportXls")
//    @RequestMapping(value = "/exportXls")
//    public ModelAndView exportXls(HttpServletRequest request, District district) {
//        return super.exportXls(request, district, District.class, "district");
//    }
//
//    /**
//      * 通过excel导入数据
//    *
//    * @param request
//    * @param response
//    * @return
//    */
//    //@RequiresPermissions("district:importExcel")
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//        return super.importExcel(request, response, District.class);
//    }

     @Cacheable(value = "sys:cache:district:alldate")
     @ApiOperation(value = "district-获取所有数据", notes = "district-获取所有数据")
     @RequestMapping(value = "getAll", method = RequestMethod.GET)
     public Result<List<District>> getAll() {
         QueryWrapper<District> queryWrapper = new QueryWrapper<>();
         queryWrapper.select("id,name");
         queryWrapper.ne("level", 2);
         List<District> list = districtService.list(queryWrapper);
         return Result.OK(list);
     }

     @ApiOperation(value = "district-根据id返回名字", notes = "district-根据id返回名字")
     @GetMapping(value = "/getNameById")
     public List<String> getNameById(@RequestParam(name = "str") String[] str) {
         QueryWrapper<District> wrapper = new QueryWrapper<>();
         wrapper.lambda().in(District::getId, str);
         wrapper.select("name");
         List<District> districts = districtService.list(wrapper);
//         List<String> list=new ArrayList<>();
////         districts.stream().collect()
//         for (District district : districts) {
//             list.add(district.getName());
//         }
         List<String> collect = districts.stream().map(District::getName).collect(Collectors.toList());
         return collect;
     }
}
